﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using Entity;

namespace CSP.BL
{
    public static class PageHelper
    {
        public static IEnumerable GetPagesWithRole(int roleID, string belongsTo)
        {
            using (var dc = new CSPEntities())
            {
                var role = dc.Roles.First(p => p.RoleID == roleID);
                var pages = dc.AspxPages.Where(p => p.BelongsTo == belongsTo).ToArray();

                role.AspxPages.Load();
                var source = from p in pages
                             select new
                             {
                                 p.PageID,
                                 p.FullName,
                                 p.AccessLevel,
                                 Accessable = role.AspxPages.Contains(p) ? "checked" : "",
                             };

                return source.ToArray();
            }
        }


        public static IEnumerable GetAspxPages()
        {
            var root = HttpContext.Current.Server.MapPath("~/");
            return RecursiveGetPages(root, "*.aspx");
        }

        private static IEnumerable RecursiveGetPages(string root, string filter)
        {
            foreach (var file in Directory.GetFiles(root, filter, SearchOption.AllDirectories))
            {
                yield return new { Name = "", FullName = "~/" + file.Substring(root.Length).Replace('\\', '/'), };
            }
        }
    }

    public enum AccessLevel
    {
        Deny = 0,
        Read = 1,
        Write = 2,
    }
}
